package clubhouse.control;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ListIterator;
import java.util.Scanner;
import java.util.Vector;



public class LandmarkGenerator {

	private int numberOfNode;
	
	private double[] xInterval = new double[] {-30, 60};
	private double[] yInterval = new double[] {-20, 40};
	
	//private double[] generatedNode = new double[] {0, 0, 0};
	//private Landmarks generationWarehouse;
	
	public LandmarkGenerator() {
		//this.generationWarehouse = new Landmarks();
	}
	
	/** 
	 * For debug only
	 * @param args
	 */
	public static void main(String[] args) {
		LandmarkGenerator test = new LandmarkGenerator();
		test.setNumberOfNode(15);
		Vector<double[]> testScenario = test.generateScenario();
		ListIterator<double[]> li = testScenario.listIterator();
		while(li.hasNext()){
			double[] node = li.next();
			System.out.println("id=\t"+node[0]+"\t\tx=\t"+node[1]+"\t\ty=\t"+node[2]);
		}
		test.writeScenario("test1", testScenario);
		System.out.println("");
		Vector<double[]> testScenario2 = test.readScenario("test1");
		ListIterator<double[]> li2 = testScenario2.listIterator();
		while(li2.hasNext()){
			double[] node = li2.next();
			System.out.println("id=\t"+node[0]+"\t\tx=\t"+node[1]+"\t\ty=\t"+node[2]);
		}
	}

	public int getNumberOfNode() {
		return numberOfNode;
	}

	public void setNumberOfNode(int numberOfNode) {
		this.numberOfNode = numberOfNode;
	}
	
	public double[][] coordinatesPool() {
		double[][] tmp = new double[2][this.getNumberOfNode()];
		
		int i;
		for(i=0; i<this.getNumberOfNode(); i++) {
			tmp[0][i] = xInterval[0] + (int)(Math.random() * ((xInterval[1] - xInterval[0]) + 1));
			tmp[1][i] = yInterval[0] + (int)(Math.random() * ((yInterval[1] - yInterval[0]) + 1));
		}
		
		return  tmp;
	}

	
	public Vector<double[]> generateScenario() {
		Vector<double[]> scenario =  new Vector<double[]>(10,10);
		
		double[][] coordinates = this.coordinatesPool();
		
		int i, id = 1;
		for(i=0; i<this.getNumberOfNode(); i++) {
			scenario.add(new double[] {id, coordinates[0][i], coordinates[1][i]});
			id++;
		}
		
		return scenario;
	}
	
	public void writeScenario(String name, Vector<double[]> data) {
		FileOutputStream fout;
		OutputStreamWriter out;
		
		try
		{
		    // Open an output stream
		    fout = new FileOutputStream (name+".txt");
		    out = new OutputStreamWriter(fout, "UTF-8");

		    ListIterator<double[]> li = data.listIterator();
			while(li.hasNext()){
				double[] node = li.next();
				out.write((int)node[0]+"\t"+(int)node[1]+"\t"+(int)node[2]+"\n");
			}

		    // Close our output stream
		    out.close();		
		}
		// Catches any error conditions
		catch (IOException e)
		{
			System.err.println ("Unable to write to file");
			System.exit(-1);
		}
		
	}
	
	public Vector<double[]> readScenario(String name){
		Vector<double[]> scenario =  new Vector<double[]>(10,10);
		try {
			Scanner scanner = new Scanner(new File(name+".txt"), "UTF-8");
			
			String entry = "";
			while (scanner.hasNextLine()){
		        //System.out.println("READ: "+scanner.nextLine());
				entry = scanner.nextLine();
				String[] data = entry.split("\t");
				scenario.add(new double[] {Integer.parseInt(data[0]), Integer.parseInt(data[1]), Integer.parseInt(data[2])});
		      }
			
			scanner.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return scenario;
	}

}
